Public Class Enemy
    Inherits Sprite
    Private intFrame As Integer
    Private intFrameLimit As Integer = 4
    Private images As New Collection()
    Private lastimage As String = "G1"
    Private objMap As Map
    Private currentGround As Integer = 0
    Private Const IMG_FOLDER As String = "../../Content/Images/"

    Public Enum SpriteDirection
        Left
        Right
    End Enum

    Public direction As SpriteDirection = SpriteDirection.Right

    Public Overloads Sub Draw(ByRef e As System.Windows.Forms.PaintEventArgs, ByVal elapsedTime As Long)
        Update(elapsedTime)
        animate()
        MyBase.Draw(e)
    End Sub
    Private Sub Update(ByVal elapsedTime As Double)
        'If Me.location.X > frmMain.Width Then
        '    Me.ptLocation.X = -Me.Bitmap.Width
        'End If
        'If Me.location.X < (-Me.Bitmap.Width) Then
        '    Me.ptLocation.X = frmMain.Width
        'End If

        currentGround = objMap.getCurrentGround(Me) - Me.Bitmap.Height

        If elapsedTime > 50000 Then
            Dim move As Double = elapsedTime / 100000
            Select Case Me.direction
                Case SpriteDirection.Right
                    Dim canmove As Integer = objMap.canMoveRight(Me, move)
                    If canmove = 0 Then
                        Me.location += New Point(move, 0)
                    Else
                        Me.location = New Point(canmove - Me.Bitmap.Width, currentGround)
                        Me.direction = SpriteDirection.Left
                    End If
                Case SpriteDirection.Left
                    Dim canmove As Integer = objMap.canMoveLeft(Me, move)
                    If canmove = 0 Then
                        Me.location -= New Point(move, 0)
                    Else
                        Me.location = New Point(canmove, currentGround)
                        Me.direction = SpriteDirection.Right
                    End If
            End Select
        End If
    End Sub
    Public Sub New(ByVal imagePath As String, ByVal location As Point, ByVal transparentColor As Color, ByVal foreground As Map)
        MyBase.New(imagePath, location, transparentColor)
        objMap = foreground
        LoadImages()
        Me.Bitmap = images("G1")
    End Sub
    Private Sub LoadImages()
        images.Add(New Bitmap(IMG_FOLDER & "koopa1.png"), "K1")
        images.Add(New Bitmap(IMG_FOLDER & "koopa2.png"), "K2")
        images.Add(New Bitmap(IMG_FOLDER & "goomba1.png"), "G1")
        images.Add(New Bitmap(IMG_FOLDER & "goomba2.png"), "G2")
        images.Add(New Bitmap(IMG_FOLDER & "goomba3.png"), "G3")
        images.Add(New Bitmap(IMG_FOLDER & "goomba4.png"), "G4")
        images.Add(New Bitmap(IMG_FOLDER & "goombaF.png"), "GF")
    End Sub
    Private Sub animate()
        intFrame += 1
        If intFrame > intFrameLimit Then
            Select Case lastimage
                Case "G1"
                    Me.Bitmap = images("G2")
                    lastimage = "G2"
                    If Me.direction = SpriteDirection.Right Then flipMe()
                Case "G2"
                    Me.Bitmap = images("G3")
                    lastimage = "G3"
                    If Me.direction = SpriteDirection.Right Then flipMe()
                Case "G3"
                    Me.Bitmap = images("G4")
                    lastimage = "G4"
                    If Me.direction = SpriteDirection.Right Then flipMe()
                Case "G4"
                    Me.Bitmap = images("G1")
                    lastimage = "G1"
                    If Me.direction = SpriteDirection.Right Then flipMe()
            End Select
        End If
        If intFrame > intFrameLimit Then intFrame = 0
    End Sub
    Private Sub flipMe()
        Dim newBmp As Bitmap = Me.Bitmap.Clone
        newBmp.RotateFlip(RotateFlipType.RotateNoneFlipX)
        Me.Bitmap = newBmp.Clone
    End Sub
End Class
